﻿/**
 * @author Administrator
 */
// 主角类出现了!!!铛铛!!
// pV是position sV是speed bR是box的Rect
function Dango(pV, bR)
{
	var _this = this;
	Sprite.call(_this, pV, bR);
	// 用于载入图像..........
	
	_this.physicsNode = new PhysicsNode(pV, bR, new Vector2(0, 0), new Vector2(0, 0));
	
	_this.imgSrc_ = ["./images/1.png",
					 "./images/3.png",
					 "./images/4.png",
					 "./images/5.png",
					 "./images/6.png"
					 ];
	_this.imgList = undefined;

	// 0代表右. 1 代表左;
	_this.faceTo = 1;
	// 这个值用于检查图片是否完全载入完成
	_this.FlipCount = 0;
	
	// 当前的动画名字:
	_this.curAct = "站立";
	// 所有图片载入完毕之后这个值会改为真.
	_this.ready = false;
	
	_this.imgLoadCallBack = function()
	{
		// 如果所有图片加载完成, 那么设置ready为真
		_this.FlipCount += 1;
//		alert(_this.FlipCount);
		if(_this.FlipCount == _this.imgSrc_.length)
		{
			_this.ready = true;
//			alert(_this.ready);
		}
	}
	
	
	
	_this.update = function() 
	{
		if(_this.physicsNode.velocity.x != 0)
		{
			if(_this.physicsNode.velocity.x > 0){
				_this.faceTo = 0;
			}
			else {
				_this.faceTo = 1;
			}
		}
	   _this.position.x = _this.physicsNode.position.x;
	   _this.position.y = _this.physicsNode.position.y;
	}
	
	_this.draw = function(canvas)
	{
		var context = canvas.getContext("2d");
		var index = 0;
		if(_this.physicsNode.velocity.y >=  -5  || _this.physicsNode.velocity.y <= 5)
		{
			index = 2;
		}
		if(_this.physicsNode.velocity.y < -5 )
		{

			if(_this.physicsNode.velocity.y < -5){
				index = 1;
			}
			if(_this.physicsNode.velocity.y < -10){
				index = 0;
			}
		}
		if(_this.physicsNode.velocity.y > 5)
		{
			//index = 3;
			if(_this.physicsNode.velocity.y > 5){
				index = 3;
			}
			if(_this.physicsNode.velocity.y > 10){
				index = 4;
			}
		}
		
		if(_this.faceTo == 1)
		{
			context.save();
			context.translate(_this.position.x + _this.box.width,_this.position.y );
			context.scale(-1.0, 1.0);
			context.drawImage(_this.imgList[index],0, 0);
			context.restore();
		}
		else
		{
			context.drawImage(_this.imgList[index], _this.position.x, _this.position.y);
		}
		
	}
	
	
	var init = function ()
	{	

		_this.imgList = new Array();
		for(var i = 0; i < _this.imgSrc_.length; ++i)
		{
			var img = new Image();
			img.src = _this.imgSrc_[i];
			img.onload = _this.imgLoadCallBack;
			_this.imgList.push(img);
		}
		

	}

	init();
}
inheritPrototype(Dango, Sprite);
